package com.newmainsoftech.ant.script;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.types.LogLevel;

public class ReadMultiLineArgs extends AntrunScriptUtilTemplate {
	public enum ArgPrefix {
		Minus( "-");
		
		private final String prefix;
			public String getPrefix() {
				return prefix;
			}
		private ArgPrefix( String prefix) {
			this.prefix = prefix;
		}
	}
	
	List<String> split( final String multilineArgs, final String regExpStr) {
		Pattern pattern = Pattern.compile( regExpStr, Pattern.MULTILINE);
		Matcher matcher = pattern.matcher( multilineArgs);
		List<String> argList = new ArrayList<String>();
		while( matcher.find()) {
			argList.add( matcher.group().trim());
		} // while
		return argList;
	}
	
	public List<String> splitByPrefix( final String multilineArgs, final ArgPrefix argPrefix) {
		if ( multilineArgs == null) {
			throw new IllegalArgumentException( "multilineArgs input cannot be null.");
		}
		if ( multilineArgs.trim().length() < 1) {
			log( 
					"Case of irregular inputs: multilineArgs input provided only white-space(s) value.", 
					LogLevel.INFO);
			List<String> argList = new ArrayList<String>();
			return argList;
		}
		
		String regExpStr;
		if ( argPrefix == null) {
			log( 
					"Value of argPrefix input is null. Thereby going to use white-space character as delimiter", 
					LogLevel.INFO);
			regExpStr = "(^(\\S|\\e)+)|([ \\t\\n\\r\\f]+(\\S|\\e)+)";
		}
		else {
			String argPrefixStr = argPrefix.getPrefix();
			regExpStr 
			= "((^" + argPrefixStr + ")|([ \\t\\n\\r\\f]+" + argPrefixStr + "))(\\S|\\e)+" 
					+ "([ \\t\\n\\r\\f]+[^" + argPrefixStr + "](\\S|\\e)*)?";
		}
		
		return split( multilineArgs, regExpStr);
	}

	/**
	 * Strip down CR, LF, TAB characters and surplus white spaces from multilineArgs. 
	 * @param multilineArgs : concatenated multiple string arguments contains CR, LF, TAB characters, 
	 * and surplus white spaces.
	 * @param argPrefix : To specify prefix character(s) of each argument
	 * @return single string after stripped down CR, LF, TAB characters, and surplus white spaces. 
	 */
	public String concatinate( 
			final String multilineArgs, final ArgPrefix argPrefix, final String concatinateStr) {
		if ( multilineArgs == null) {
			throw new IllegalArgumentException( "multilineArgs input cannot be null.");
		}
		if ( multilineArgs.length() < 1) {
			log( "Case of irregular input: value of multilineArgs input is empty string", LogLevel.INFO);
			return "";
		}
		String concatinateStrCopy = concatinateStr;
		if ( concatinateStrCopy == null) {
			log( 
					"Going to use a space as default delimitor due to " 
							+ "concatinateStr input provided null value.", 
					LogLevel.INFO);
			concatinateStrCopy = " ";
		}
		else if ( "".equals( concatinateStrCopy)) {
			log( 
					"Case of irregular input: going to use empty space as delimitor " 
							+ "due to concatinateStr input provided it.", 
					LogLevel.WARN);
		}
		
		String concatinatedArgsStr = "";
		for( String argStr : splitByPrefix( multilineArgs, argPrefix)) {
			concatinatedArgsStr = concatinatedArgsStr + concatinateStr + argStr;
		}
		return concatinatedArgsStr.substring( concatinateStr.length());
	}
}
